package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.GestureImageView;

public class KnowledgeForumPictureFragment extends BaseFragment {

  public static final String EXTRA_PICTURE = "com.isunland.managesystem.ui.EXTRA_PICTURE";

  private String mPicture;
  private ImageLoader mImageLoader;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.pictureDetail);
    mImageLoader = RequestManager.getImageLoader();
    mPicture = getArguments().getString(EXTRA_PICTURE);
    if (mActivity.getSupportActionBar() != null) {
      mActivity.getSupportActionBar().hide();
    }
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_forum_picture, container, false);
    final GestureImageView mIvForumPicture =
        (GestureImageView) view.findViewById(R.id.iv_forum_picture);

    if (!TextUtils.isEmpty(mPicture)) {
      String urlImage = ApiConst.getDownLoadUrlByGet(mPicture);
      //ImageListener listener = ImageLoader.getImageListener(mIvForumPicture, // 控件名
      //    0, 0); // 默认图片
      MyUtils.showDialogLoading(getActivity(), "图片加载中...");
      ImageListener listener = new ImageListener() {
        @Override public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
          if (response.getBitmap() != null) {
            MyUtils.dismissDialog();
            mIvForumPicture.setImageBitmap(response.getBitmap());
          }
        }

        @Override public void onErrorResponse(VolleyError error) {
          MyUtils.dismissDialog();
          ToastUtil.show("图片加载失败!");
          mIvForumPicture.setImageResource(R.drawable.fail_pic);
        }
      };
      mImageLoader.get(urlImage, listener);
    }

    mIvForumPicture.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        getActivity().finish();
      }
    });
    return view;
  }

  public static Fragment newInstance(String picture) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_PICTURE, picture);
    KnowledgeForumPictureFragment fragment = new KnowledgeForumPictureFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  /**
   * 响应ActionBar中的按钮
   */
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮

      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
